#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int SeqListDataType;
typedef struct SeqList 
{
    SeqListDataType* data;
    size_t size;
    size_t capacity;
}SeqList;

void SeqListInit(SeqList* ps)
{
  assert(ps);
  SeqListDataType* temp = (SeqListDataType*)malloc(sizeof(SeqListDataType) * 4);
  if(temp == NULL)
  {
    perror("malloc failed");
    exit(-1);
  }
  
  ps->data = temp;
  ps->size = 0;
  ps->capacity = 4;
}

void SeqListDestory(SeqList* ps)
{
  assert(ps);
  free(ps->data);
  ps->data = NULL;
  ps->size = ps->capacity = 0;
}

void SeqListCheckCapacity(SeqList* ps)
{
  assert(ps);
  if(ps->size == ps->capacity)
  {
    size_t newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    SeqListDataType* temp = (SeqListDataType*)realloc(ps->data, sizeof(SeqListDataType) * newcapacity);
    if(temp == NULL)
    {
      perror("realloc failed");
      return;
    }
    
    ps->data = temp;
    ps->capacity = newcapacity;
  }

}

void SeqListPrint(SeqList* ps)
{
  assert(ps);
  size_t i = 0;
  for(i = 0; i < ps->size; i++)
  {
    printf("%d ", ps->data[i]);
  }
  
  printf("\n");
}


void SeqListPushBack(SeqList* ps, SeqListDataType x)
{
  assert(ps);
  SeqListCheckCapacity(ps);
  ps->data[ps->size] = x;
  ps->size++;
}

void SeqListPopBack(SeqList* ps)
{
  assert(ps);
  assert(ps->size > 0);
  ps->size--;
}

void SeqListPushFront(SeqList* ps, SeqListDataType x)
{
  assert(ps);
  SeqListCheckCapacity(ps);
  
  int i = 0;
  for(i = ps->size - 1; i >= 0; i--)
  {
    ps->data[i + 1] = ps->data[i];
  }

  ps->data[0] = x;
  ps->size++;
}

void SeqListPopFront(SeqList* ps)
{
  assert(ps);
  
  int i = 0;
  for(i = 0; i <(int) (ps->size - 1); i++)
  {
    ps->data[i] = ps->data[i + 1];
  }
  ps->size--;
}


int SeqListFind(SeqList* ps, SeqListDataType x)
{
  assert(ps);
  
  int i = 0;
  for( i = 0; i < (int)(ps->size); i++)
  {
      if(ps->data[i] == x)
        return x;
  }
  
  return -1;
}

void SeqListInsert(SeqList* ps, size_t pos, SeqListDataType x)
{
  assert(ps);
  assert(pos <= ps->size);
  SeqListCheckCapacity(ps);

  size_t i = 0;
  for(i = ps->size; i <= pos; i--)
  {
    ps->data[i] = ps->data[i -1];
  }

  ps->data[pos] = x;
  ps->size++;
}

void SeqListErase(SeqList* ps, size_t pos)
{
    assert(ps);
    // 检查删除位置的有效性。pos 必须在 [0, size - 1] 范围内。
    // 如果 pos == size，则表示索引越界。
    assert(pos < ps->size);

    // 从 pos + 1 位置开始，向左移动所有元素，覆盖 pos 位置的元素。
    // 使用 size_t 进行无符号循环，确保循环条件正确。
    

    for (size_t i = pos + 1; i < ps->size; i++)
    {
        ps->data[i - 1] = ps->data[i];
    }

    // 元素数量减一
    ps->size--;
}





int main()
{

  SeqList seqlist;
  SeqListInit(&seqlist);
  SeqListPushBack(&seqlist, 11);
  SeqListPushBack(&seqlist, 12);
  SeqListPushBack(&seqlist, 13);
  SeqListPushBack(&seqlist, 14);
  SeqListPushBack(&seqlist, 15);


  SeqListPrint(&seqlist);

  SeqListPushFront(&seqlist, 1);
  SeqListPushFront(&seqlist, 2);
  SeqListPushFront(&seqlist, 3);
  SeqListPushFront(&seqlist, 4);
  SeqListPushFront(&seqlist, 5);

  SeqListPrint(&seqlist);

  SeqListPopFront(&seqlist);
  SeqListPopFront(&seqlist);
  SeqListPopFront(&seqlist);
  SeqListPopFront(&seqlist);
  SeqListPopFront(&seqlist);
  
  SeqListPrint(&seqlist);
/*
  printf("%zd \n", seqlist.capacity);
  printf("%zd \n", seqlist.size);
*/

  SeqListDestory(&seqlist);






  return 0;
}

